package org.pb.pay.service.impl;

import org.pb.basic.service.impl.BaseServiceImpl;
import org.pb.basic.utils.AliPayUtils;
import org.pb.pay.domain.AlipayInfo;
import org.pb.pay.domain.PayBill;
import org.pb.pay.mapper.AlipayInfoMapper;
import org.pb.pay.mapper.PayBillMapper;
import org.pb.pay.query.PayBillQuery;
import org.pb.pay.service.IPayBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支付账单的业务层实现类
 * @author pb
 */
@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill, PayBillQuery> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;


    @Override
    public String getResultData(PayBill payBillTemp) {
        if (payBillTemp == null) {
            throw new RuntimeException("支付账单信息为空，请确认支付账单信息");
        }
        // 为了防止账单最后支付时间过期，重新查询账单
        PayBill payBill = payBillMapper.loadByUnionPaySn(payBillTemp.getUnionPaySn());
        // 如果得到的账单信息为空，表示账单过期，需要重新生成订单
        if (payBill == null) {
            throw new RuntimeException("支付账单已过期，请重新下单");
        }
        String resultData = null;
        switch (payBill.getPayChannel().intValue()) {
            case 1 :
                // 支付宝支付,获取到对应店铺的支付宝秘钥信息
                AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                // 通过工具类生成结果数据集
                resultData = AliPayUtils.pay(alipayInfo, payBill);
                break;
            case 2 :
                // 微信支付
                break;
            case 3 :
                // 银联支付
                break;
            default:
                // 余额支付
                break;
        }
        return resultData;
    }

    @Override
    public PayBill getByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
